﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Domain.Model;
using Resto.CostControl.Core.Cache;
using Resto.CostControl.Core.Enums;

namespace Resto.CostControl.Core.Facade.SiteModule.DataTransferObject
{
    /// <summary>
    /// 项目点成本
    /// </summary>
    [Serializable]
    public class SiteCostDTO
    {
        #region 构造函数

        public SiteCostDTO() { }

        internal SiteCostDTO(SiteCost cost)
        {
            this.ID = cost.ID;
            this.BeginDate = cost.BeginDate;
            this.EndDate = cost.EndDate;
            this.SiteId = cost.SiteID;
            this.State = (DataState)Enum.Parse(typeof(DataState),cost.State);


            List<SiteCostItemDTO> list = new List<SiteCostItemDTO>();
            foreach (SiteCostItem item in cost.Items)
                list.Add(new SiteCostItemDTO(item));

            this.Items = list.ToArray();

        }

        #endregion

        #region 基本属性

        public string ID { get; set; }

        /// <summary>
        /// 开始日期
        /// </summary>
        public DateTime BeginDate { get; set; }

        /// <summary>
        /// 结束日期
        /// </summary>
        public DateTime EndDate { get; set; }

        /// <summary>
        /// 预计成本
        /// </summary>
        public decimal ExpectedCost
        {
            get
            {
                decimal value = 0m;

                foreach (SiteCostItemDTO item in Items)
                    value += item.ExpectedCost;

                return value;
            }
        }

        /// <summary>
        /// 实际成本
        /// </summary>
        public decimal? ActualCost
        {
            get
            {
                decimal value = 0m;

                foreach (SiteCostItemDTO item in Items)
                {
                    if (item.ActualCost.HasValue)
                        value += item.ActualCost.Value;
                }

                if (value > 0m)
                    return value;
                else
                    return null;
            }
        }

        /// <summary>
        /// 记录的状态,UI不要赋值
        /// </summary>
        public DataState State { get; set; }

        /// <summary>
        /// 项目点Id
        /// </summary>
        public string SiteId { get; set; }

        /// <summary>
        /// 项目点名称
        /// </summary>
        public string SiteName
        {
            get
            {
                SiteDTO siteDTO = SiteDTOCache.Get(SiteId);
                if (siteDTO != null)
                    return siteDTO.Name;
                else
                    return string.Empty;
            }
        }

        /// <summary>
        /// 成本子项
        /// </summary>
        public SiteCostItemDTO[] Items { get; set; }

        #endregion

        #region 方法

        internal SiteCost ToSiteCost()
        {
            SiteCost order = new SiteCost();
            CopyToSiteCost(order);
            return order;
        }

        internal void CopyToSiteCost(SiteCost cost)
        {
            cost.BeginDate = this.BeginDate.Date;
            cost.EndDate = this.EndDate.Date;
            cost.SiteID = this.SiteId;
            cost.ExpectedCost = this.ExpectedCost;
            cost.ActualCost = this.ActualCost;
            

            //清除要删除的项
            string[] oldIds = (from o in cost.Items
                            select o.ID).ToArray();

            string[] newIds = (from o in this.Items
                            select o.ID).ToArray();

            List<string> delIds = (from o in oldIds
                                where !newIds.Contains(o)
                                select o).ToList();

            delIds.ForEach(x => cost.RemoveItem(x));


            //修改和新增项
            foreach (SiteCostItemDTO itemDTO in Items)
            {
                SiteCostItem item;
                if (!string.IsNullOrWhiteSpace(itemDTO.ID))
                {
                    item = cost.GetItem(itemDTO.ID);
                    if (item != null)
                        itemDTO.CopyToSiteCostItem(item);
                }
                else
                    cost.AddItem(itemDTO.ToSiteCostItem());
            }
        }

        #endregion
    }
}
